<template>
  <el-container>
    <el-aside width="500px">
      <el-form size="mini" :model="gms" :inline="true">
        <el-form-item v-for="(val, key) in gms" :key="key">
          <el-button @click="getInfos(key)">{{ val }}</el-button>
        </el-form-item>
      </el-form>
    </el-aside>
    <el-aside width="500px">
      <el-form :model="infos" label-width="100px">
        <!-- <el-form-item
          v-for="(info, index) in infos"
          :label="info.label"
          :key="index"
        >
          <el-input
            type="textarea"
            :rows="1"
            placeholder="请输入内容"
            v-model="info.value"
          ></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-input type="textarea" autosize v-model="info"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="upParams">执行</el-button>
        </el-form-item>
      </el-form>
    </el-aside>
    <el-aside width="500px">
      <el-form>
        <el-form-item>
          <el-input type="textarea" autosize v-model="result"></el-input>
        </el-form-item>
      </el-form>
    </el-aside>
  </el-container>
</template>

<script>
import { jsModuleInfoReq, jsMoudleParamReq, jsModuleExecReq } from "../api/req";
export default {
  data() {
    let gms = {
      1: "hello",
      2: "world",
    };
    let infos = [];
    infos.push({
      label: "hello",
      value: "world",
    });
    return {
      gms: gms,
      infos: infos,
      info: "",
      result: "hello world",
    };
  },

  methods: {
    getInfos(key) {
      let val = this.gms[key];
      let req = {
        fileName: val,
      };
      this.$store.commit("changeJsModule", key);
      jsMoudleParamReq(req).then((res) => {
        /*
        this.infos = [];
        for (let k in res) {
          this.infos.push({
            label: k,
            value: res[k],
          });
        }
        */
        this.info = JSON.stringify(res, null, 2);
      });
    },
    upParams() {
      let key = this.$store.state.jsModuleKey;
      let fileName = this.gms[key];
      if (!fileName) this.$message.error("no fileName");
      let reqInfo = JSON.parse(this.info);
      if (!reqInfo.__execFileName)
        reqInfo.__execFileName = fileName;
      jsModuleExecReq(reqInfo).then((res) => {
        this.result = JSON.stringify(res, null, 2);
      });
    },
  },

  created() {
    jsModuleInfoReq().then((res) => {
      this.gms = res;
    });
  },
};
</script>
